﻿using MVCWorkflow.Models;
using MVCWorkflow.Workflows;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCWorkflow.Controllers
{
    public class ArticlesController : Controller
    {
        private DataContext db = new DataContext();

        //
        // GET: /ArticlesNew/

        public ActionResult Index()
        {
            return View(db.Articles.ToList());
        }

        //
        // GET: /ArticlesNew/Details/5

        public ActionResult Details(int id = 0)
        {
            Article article = db.Articles.Find(id);
            if (article == null)
            {
                return HttpNotFound();
            }
            return View(article);
        }

        //
        // GET: /ArticlesNew/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /ArticlesNew/Create

        [HttpPost]
        public ActionResult Create(Article article)
        {
            ModelState.Remove("Published");
            ModelState.Remove("WorkflowID");
            ModelState.Remove("State");

            if (ModelState.IsValid)
            {
                article.State = "New";

                db.Articles.Add(article);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(article);
        }

        //
        // GET: /ArticlesNew/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Article article = db.Articles.Find(id);
            if (article == null)
            {
                return HttpNotFound();
            }
            return View(article);
        }

        //
        // POST: /ArticlesNew/Edit/5

        [HttpPost]
        public ActionResult Edit(Article article)
        {
            ModelState.Remove("Published");
            ModelState.Remove("WorkflowID");
            ModelState.Remove("State");

            if (ModelState.IsValid)
            {
                db.Entry(article).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(article);
        }

        //
        // GET: /ArticlesNew/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Article article = db.Articles.Find(id);
            if (article == null)
            {
                return HttpNotFound();
            }
            return View(article);
        }

        //
        // POST: /ArticlesNew/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Article article = db.Articles.Find(id);
            db.Articles.Remove(article);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        [HttpPost]
        public ActionResult Submit(int articleID)
        {
            var activity = new PublishArticle();
            activity.ArticleID = articleID;

            var wfHost = new WorkflowHost(activity);
            var instanceID = wfHost.Start();

            wfHost.Unload();

            Article article = db.Articles.Single(e => e.WorkflowID == instanceID);

            return RedirectToAction(article.State, new { workflowID = instanceID });
}

public ActionResult Evaluate(Guid workflowID)
{
    return View(workflowID);
}

[HttpPost]
public ActionResult Evaluate(Guid workflowID, string action)
{
    var activity = new PublishArticle();
    var wfHost = new WorkflowHost(activity);
    wfHost.Resume(workflowID);
    wfHost.Run(workflowID, action);
    wfHost.Unload();

    Article article = db.Articles.Single(e => e.WorkflowID == workflowID);

    return RedirectToAction(article.State, new { workflowID = workflowID });
}

public ActionResult Published(Guid workflowID)
{
    return View();
}

public ActionResult Retained(Guid workflowID)
{
    return View();
}
    }
}
